<template>
  <div class="friend-verification">
    <!-- 头部区域 -->
    <div class="header-section">
      <div class="header-card">
        <div class="header-content">
          <div class="header-icon">
            <i class="el-icon-user-solid"></i>
          </div>
          <div class="header-info">
            <h2 class="header-title">新的好友</h2>
            <p class="header-subtitle">好友申请和验证消息</p>
          </div>
          <div class="header-actions">
            <el-button 
              type="text" 
              icon="el-icon-refresh" 
              @click="refresh"
              :loading="loading"
              class="refresh-button"
            >
              刷新
            </el-button>
          </div>
        </div>
      </div>
    </div>

    <!-- 搜索和统计区域 -->
    <div class="search-stats-section">
      <div class="search-card">
        <el-input
          v-model="searchKeyword"
          placeholder="搜索好友申请..."
          prefix-icon="el-icon-search"
          size="medium"
          clearable
          @input="handleSearch"
          class="search-input"
        />
      </div>
      
      <div class="stats-card" v-if="friendRequests.length > 0">
        <div class="stats-grid">
          <div class="stat-item">
            <div class="stat-number">{{ friendRequests.length }}</div>
            <div class="stat-label">总申请</div>
          </div>
          <div class="stat-item">
            <div class="stat-number unread">{{ unreadCount }}</div>
            <div class="stat-label">未读</div>
          </div>
          <div class="stat-item">
            <div class="stat-number pending">{{ pendingCount }}</div>
            <div class="stat-label">待处理</div>
          </div>
        </div>
      </div>
    </div>
    
    <!-- 好友申请列表 -->
    <div class="requests-section">
      <div v-if="loading" class="loading-container">
        <div class="loading-content">
          <div class="loading-spinner"></div>
          <p class="loading-text">加载中...</p>
        </div>
      </div>
      
      <div v-else-if="filteredRequests.length === 0" class="empty-container">
        <div class="empty-content">
          <div class="empty-icon">
            <i class="el-icon-user"></i>
          </div>
          <h3 class="empty-title">暂无好友申请</h3>
          <p class="empty-description">当有人申请添加您为好友时，会在这里显示</p>
        </div>
      </div>
      
      <div v-else class="requests-list">
        <div
          v-for="request in filteredRequests"
          :key="request.id"
          class="request-card"
          :class="{ 
            'unread': !request.isRead,
            'pending': request.status === 'pending',
            'accepted': request.status === 'accepted',
            'rejected': request.status === 'rejected'
          }"
        >
          <div class="request-avatar">
            <img
              :src="request.avatar || defaultAvatar"
              :alt="request.nickname"
              @error="handleAvatarError"
            />
            <div v-if="!request.isRead" class="unread-indicator"></div>
            <div v-if="request.status === 'pending'" class="status-indicator pending"></div>
            <div v-if="request.status === 'accepted'" class="status-indicator accepted">
              <i class="el-icon-check"></i>
            </div>
            <div v-if="request.status === 'rejected'" class="status-indicator rejected">
              <i class="el-icon-close"></i>
            </div>
          </div>
          
          <div class="request-content">
            <div class="request-header">
              <div class="user-info">
                <h4 class="user-name">{{ request.nickname }}</h4>
                <span class="user-id">ID: {{ request.friendId }}</span>
              </div>
              <span class="request-time">{{ formatTime(request.createTime) }}</span>
            </div>
            
            <div class="request-message">
              <div class="message-bubble">
                <p class="message-text">{{ request.message || '请求添加您为好友' }}</p>
              </div>
            </div>
            
            <div class="request-actions">
              <el-button
                v-if="request.status === 'pending'"
                type="primary"
                size="small"
                @click="handleAccept(request)"
                :loading="request.processing"
                class="action-button accept-button"
              >
                <i class="el-icon-check"></i>
                同意
              </el-button>
              <el-button
                v-if="request.status === 'pending'"
                type="danger"
                size="small"
                @click="handleReject(request)"
                :loading="request.processing"
                class="action-button reject-button"
              >
                <i class="el-icon-close"></i>
                拒绝
              </el-button>
              <div v-if="request.status === 'accepted'" class="status-badge accepted">
                <i class="el-icon-check"></i>
                已同意
              </div>
              <div v-if="request.status === 'rejected'" class="status-badge rejected">
                <i class="el-icon-close"></i>
                已拒绝
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: 'FriendVerification',
  data() {
    return {
      loading: false,
      searchKeyword: '',
      friendRequests: [],
      defaultAvatar: require('@/assets/icon/default-avatar.svg').default
    };
  },
  computed: {
    filteredRequests() {
      if (!this.searchKeyword) {
        return this.friendRequests;
      }
      return this.friendRequests.filter(request => 
        request.nickname.toLowerCase().includes(this.searchKeyword.toLowerCase()) ||
        request.friendId.toLowerCase().includes(this.searchKeyword.toLowerCase())
      );
    },
    totalCount() {
      return this.friendRequests.length;
    },
    unreadCount() {
      return this.friendRequests.filter(request => !request.isRead).length;
    },
    pendingCount() {
      return this.friendRequests.filter(request => request.status === 'pending').length;
    }
  },
  mounted() {
    this.loadFriendRequests();
  },
  methods: {
    async loadFriendRequests() {
      this.loading = true;
      try {
        // 模拟数据
        this.friendRequests = [
          {
            id: 1,
            friendId: 'user001',
            nickname: '张三',
            avatar: '',
            message: '你好，我想添加您为好友',
            status: 'pending',
            isRead: false,
            createTime: new Date().getTime() - 3600000,
            processing: false
          },
          {
            id: 2,
            friendId: 'user002',
            nickname: '李四',
            avatar: '',
            message: '请求添加您为好友',
            status: 'accepted',
            isRead: true,
            createTime: new Date().getTime() - 7200000,
            processing: false
          },
          {
            id: 3,
            friendId: 'user003',
            nickname: '王五',
            avatar: '',
            message: '通过朋友介绍认识',
            status: 'pending',
            isRead: false,
            createTime: new Date().getTime() - 1800000,
            processing: false
          }
        ];
      } catch (error) {
        console.error('加载好友申请失败:', error);
        this.$message.error('加载好友申请失败');
      } finally {
        this.loading = false;
      }
    },
    async refresh() {
      await this.loadFriendRequests();
      this.$message.success('刷新成功');
    },
    handleSearch() {
      // 搜索逻辑已在computed中处理
    },
    async handleAccept(request) {
      request.processing = true;
      try {
        // 模拟API调用
        await new Promise(resolve => setTimeout(resolve, 1000));
        request.status = 'accepted';
        request.isRead = true;
        this.$message.success('已同意好友申请');
      } catch (error) {
        this.$message.error('操作失败');
      } finally {
        request.processing = false;
      }
    },
    async handleReject(request) {
      request.processing = true;
      try {
        // 模拟API调用
        await new Promise(resolve => setTimeout(resolve, 1000));
        request.status = 'rejected';
        request.isRead = true;
        this.$message.success('已拒绝好友申请');
      } catch (error) {
        this.$message.error('操作失败');
      } finally {
        request.processing = false;
      }
    },
    handleAvatarError(event) {
      event.target.src = this.defaultAvatar;
    },
    formatTime(timestamp) {
      const now = new Date().getTime();
      const diff = now - timestamp;
      const minutes = Math.floor(diff / 60000);
      const hours = Math.floor(diff / 3600000);
      const days = Math.floor(diff / 86400000);
      
      if (minutes < 1) return '刚刚';
      if (minutes < 60) return `${minutes}分钟前`;
      if (hours < 24) return `${hours}小时前`;
      if (days < 7) return `${days}天前`;
      
      return new Date(timestamp).toLocaleDateString();
    }
  }
};
</script>

<style scoped>
.friend-verification {
  width: 100%;
  max-width: 600px;
  margin: 0 auto;
  padding: 20px;
  background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
  min-height: 100vh;
}

/* 头部区域 */
.header-section {
  margin-bottom: 20px;
}

.header-card {
  background: white;
  border-radius: 16px;
  padding: 24px;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.08);
  border: 1px solid rgba(255, 255, 255, 0.2);
}

.header-content {
  display: flex;
  align-items: center;
  gap: 16px;
}

.header-icon {
  width: 56px;
  height: 56px;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  border-radius: 16px;
  display: flex;
  align-items: center;
  justify-content: center;
  color: white;
  font-size: 24px;
  box-shadow: 0 4px 15px rgba(102, 126, 234, 0.3);
}

.header-info {
  flex: 1;
}

.header-title {
  margin: 0 0 4px 0;
  font-size: 24px;
  font-weight: 700;
  color: #2c3e50;
  background: linear-gradient(135deg, #667eea, #764ba2);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
}

.header-subtitle {
  margin: 0;
  color: #7f8c8d;
  font-size: 14px;
  font-weight: 500;
}

.header-actions {
  margin-left: auto;
}

.refresh-button {
  color: #667eea;
  font-weight: 600;
  padding: 8px 16px;
  border-radius: 8px;
  transition: all 0.3s ease;
}

.refresh-button:hover {
  background: rgba(102, 126, 234, 0.1);
  transform: translateY(-1px);
}

/* 搜索和统计区域 */
.search-stats-section {
  margin-bottom: 24px;
}

.search-card {
  background: white;
  border-radius: 16px;
  padding: 20px;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.08);
  margin-bottom: 16px;
}

.search-input :deep(.el-input__inner) {
  border-radius: 12px;
  border: 2px solid #e1e8ed;
  padding: 12px 16px;
  font-size: 14px;
  transition: all 0.3s ease;
}

.search-input :deep(.el-input__inner:focus) {
  border-color: #667eea;
  box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.1);
}

.stats-card {
  background: white;
  border-radius: 16px;
  padding: 20px;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.08);
}

.stats-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 20px;
}

.stat-item {
  text-align: center;
  padding: 16px;
  border-radius: 12px;
  background: linear-gradient(135deg, #f8f9fa 0%, #e9ecef 100%);
  transition: all 0.3s ease;
}

.stat-item:hover {
  transform: translateY(-2px);
  box-shadow: 0 8px 25px rgba(0, 0, 0, 0.1);
}

.stat-number {
  font-size: 28px;
  font-weight: 700;
  color: #2c3e50;
  margin-bottom: 4px;
}

.stat-number.unread {
  color: #e74c3c;
}

.stat-number.pending {
  color: #f39c12;
}

.stat-label {
  font-size: 12px;
  color: #7f8c8d;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.5px;
}

/* 申请列表区域 */
.requests-section {
  display: flex;
  flex-direction: column;
  gap: 16px;
}

/* 加载状态 */
.loading-container {
  display: flex;
  justify-content: center;
  align-items: center;
  padding: 60px 0;
}

.loading-content {
  text-align: center;
}

.loading-spinner {
  width: 40px;
  height: 40px;
  border: 4px solid #f3f3f3;
  border-top: 4px solid #667eea;
  border-radius: 50%;
  animation: spin 1s linear infinite;
  margin: 0 auto 16px;
}

@keyframes spin {
  0% { transform: rotate(0deg); }
  100% { transform: rotate(360deg); }
}

.loading-text {
  color: #7f8c8d;
  font-size: 14px;
  font-weight: 500;
  margin: 0;
}

/* 空状态 */
.empty-container {
  display: flex;
  justify-content: center;
  align-items: center;
  padding: 60px 0;
}

.empty-content {
  text-align: center;
}

.empty-icon {
  width: 80px;
  height: 80px;
  background: linear-gradient(135deg, #f8f9fa 0%, #e9ecef 100%);
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  margin: 0 auto 20px;
  color: #adb5bd;
  font-size: 32px;
}

.empty-title {
  font-size: 18px;
  font-weight: 600;
  color: #2c3e50;
  margin: 0 0 8px 0;
}

.empty-description {
  color: #7f8c8d;
  font-size: 14px;
  margin: 0;
  line-height: 1.5;
}

/* 申请列表 */
.requests-list {
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.request-card {
  background: white;
  border-radius: 16px;
  padding: 20px;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.08);
  border: 2px solid transparent;
  transition: all 0.3s ease;
  position: relative;
  overflow: hidden;
}

.request-card::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  height: 4px;
  background: linear-gradient(135deg, #667eea, #764ba2);
  opacity: 0;
  transition: opacity 0.3s ease;
}

.request-card:hover {
  transform: translateY(-2px);
  box-shadow: 0 8px 30px rgba(0, 0, 0, 0.12);
}

.request-card:hover::before {
  opacity: 1;
}

.request-card.unread {
  border-color: #667eea;
  background: linear-gradient(135deg, rgba(102, 126, 234, 0.05) 0%, rgba(118, 75, 162, 0.05) 100%);
}

.request-card.unread::before {
  opacity: 1;
}

.request-card.pending {
  border-color: #f39c12;
}

.request-card.accepted {
  border-color: #27ae60;
}

.request-card.rejected {
  border-color: #e74c3c;
}

.request-card {
  display: flex;
  gap: 16px;
  align-items: flex-start;
}

.request-avatar {
  position: relative;
  width: 60px;
  height: 60px;
  border-radius: 16px;
  overflow: hidden;
  flex-shrink: 0;
  box-shadow: 0 4px 15px rgba(0, 0, 0, 0.1);
}

.request-avatar img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.unread-indicator {
  position: absolute;
  top: -2px;
  right: -2px;
  width: 16px;
  height: 16px;
  background: #e74c3c;
  border-radius: 50%;
  border: 3px solid white;
  animation: pulse 2s infinite;
}

@keyframes pulse {
  0% { transform: scale(1); }
  50% { transform: scale(1.1); }
  100% { transform: scale(1); }
}

.status-indicator {
  position: absolute;
  bottom: -2px;
  right: -2px;
  width: 20px;
  height: 20px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  border: 3px solid white;
  font-size: 10px;
  color: white;
}

.status-indicator.pending {
  background: #f39c12;
}

.status-indicator.accepted {
  background: #27ae60;
}

.status-indicator.rejected {
  background: #e74c3c;
}

.request-content {
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.request-header {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
}

.user-info {
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.user-name {
  margin: 0;
  font-size: 16px;
  font-weight: 600;
  color: #2c3e50;
}

.user-id {
  font-size: 12px;
  color: #7f8c8d;
  font-family: 'Monaco', 'Menlo', monospace;
  background: #f8f9fa;
  padding: 2px 6px;
  border-radius: 4px;
}

.request-time {
  font-size: 12px;
  color: #95a5a6;
  font-weight: 500;
  white-space: nowrap;
}

.request-message {
  margin: 4px 0;
}

.message-bubble {
  background: linear-gradient(135deg, #f8f9fa 0%, #e9ecef 100%);
  border-radius: 12px;
  padding: 12px 16px;
  border: 1px solid #e9ecef;
  position: relative;
}

.message-bubble::before {
  content: '';
  position: absolute;
  top: 8px;
  left: -6px;
  width: 0;
  height: 0;
  border-top: 6px solid transparent;
  border-bottom: 6px solid transparent;
  border-right: 6px solid #e9ecef;
}

.message-text {
  margin: 0;
  color: #495057;
  font-size: 14px;
  line-height: 1.5;
}

.request-actions {
  display: flex;
  gap: 12px;
  align-items: center;
  margin-top: 8px;
}

.action-button {
  border-radius: 8px;
  font-weight: 600;
  padding: 8px 16px;
  font-size: 13px;
  transition: all 0.3s ease;
  border: none;
  display: flex;
  align-items: center;
  gap: 4px;
}

.accept-button {
  background: linear-gradient(135deg, #27ae60, #2ecc71);
  color: white;
  box-shadow: 0 4px 15px rgba(39, 174, 96, 0.3);
}

.accept-button:hover {
  background: linear-gradient(135deg, #229954, #27ae60);
  transform: translateY(-1px);
  box-shadow: 0 6px 20px rgba(39, 174, 96, 0.4);
}

.reject-button {
  background: linear-gradient(135deg, #e74c3c, #c0392b);
  color: white;
  box-shadow: 0 4px 15px rgba(231, 76, 60, 0.3);
}

.reject-button:hover {
  background: linear-gradient(135deg, #c0392b, #a93226);
  transform: translateY(-1px);
  box-shadow: 0 6px 20px rgba(231, 76, 60, 0.4);
}

.status-badge {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 6px 12px;
  border-radius: 20px;
  font-size: 12px;
  font-weight: 600;
}

.status-badge.accepted {
  background: linear-gradient(135deg, rgba(39, 174, 96, 0.1), rgba(46, 204, 113, 0.1));
  color: #27ae60;
  border: 1px solid rgba(39, 174, 96, 0.2);
}

.status-badge.rejected {
  background: linear-gradient(135deg, rgba(231, 76, 60, 0.1), rgba(192, 57, 43, 0.1));
  color: #e74c3c;
  border: 1px solid rgba(231, 76, 60, 0.2);
}

/* 响应式设计 */
@media (max-width: 768px) {
  .friend-verification {
    padding: 16px;
  }
  
  .header-content {
    flex-direction: column;
    text-align: center;
    gap: 12px;
  }
  
  .header-actions {
    margin-left: 0;
  }
  
  .stats-grid {
    grid-template-columns: 1fr;
    gap: 12px;
  }
  
  .request-card {
    flex-direction: column;
    gap: 16px;
  }
  
  .request-actions {
    flex-direction: column;
    align-items: stretch;
  }
  
  .action-button {
    width: 100%;
    justify-content: center;
  }
}
</style>